package com.szosen.homeKey;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;

import com.szosen.jkzj.R;
import com.szosen.jkzj.activity.LoginActivity;
import com.szosen.jkzj.activity.MainActivity;

/**
 * custom launcher home Note: This Activity is not the lock-screen page,but it
 * is important to help implementing lock screen.
 * 
 * @author Andy
 */
public class LauncherHomeActivity extends Activity {
	private String mPackageName;
	private String mClassName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.activity_launcher_home);
	}

	@Override
	protected void onResume() {
		super.onResume();
		Intent intent = new Intent(this, LockScreenService.class);
		startService(intent);
		// 按了home键就来了这个homeActivity。然后就可以做这个事情了。
		// 启动主界面
		PackageManager manager = this.getPackageManager();
		Intent openQQ = manager.getLaunchIntentForPackage("com.szosen.jkzj");
		this.startActivity(openQQ);
		// intent = new Intent(this, LoginActivity.class);
		// startActivity(intent);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
	}

	/**
	 * get the system launcher package name and class name,we use system
	 * launcher as default<br/>
	 * also we can let user to choose a launcher from system laucher and our
	 * custom launchers,"BaiDu" lock screen app and "Go" lock screen app just do
	 * this.<br/>
	 * 
	 * @param context
	 */
	private void getLauncherPackageName(Context context) {
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_HOME);
		List<ResolveInfo> resInfoList = context.getPackageManager()
				.queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);
		if (resInfoList != null) {
			ResolveInfo resInfo;
			for (int i = 0; i < resInfoList.size(); i++) {
				resInfo = resInfoList.get(i);
				if ((resInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
					mPackageName = resInfo.activityInfo.packageName;
					mClassName = resInfo.activityInfo.name;
					break;
				}
			}
		}
	}

	/**
	 * 获取正在运行桌面包名（注：存在多个桌面时且未指定默认桌面时，该方法返回Null,使用时需处理这个情况）
	 */
	public static String getLauncherPackageName2(Context context) {
		final Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_HOME);
		final ResolveInfo res = context.getPackageManager().resolveActivity(
				intent, 0);
		if (res.activityInfo == null) {
			// should not happen. A home is always installed, isn't it?
			return null;
		}
		if (res.activityInfo.packageName.equals("android")) {
			// 有多个桌面程序存在，且未指定默认项时；
			return null;
		} else {
			return res.activityInfo.packageName;
		}
	}
}
